/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utility.com;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
public final class GradientPanel extends javax.swing.JPanel {

    /**
     * Creates new form GradientPanel
     */
    public GradientPanel() {
     //   initComponents();
         this( new FlowLayout(), true );
    }
    
      protected boolean isDark;
 
    public GradientPanel( LayoutManager layout, boolean isDoubleBuffered ) {
        super( layout, isDoubleBuffered );
        setOpaque( true );
        setDark( false );
    }
 
    public GradientPanel( LayoutManager layout ) {
        this( layout, true );
    }
 
    public GradientPanel( boolean isDoubleBuffered ) {
        this( new FlowLayout(), isDoubleBuffered );
    }
 
 
 
    public void setDark( boolean b ) {
        isDark = b;
    }
 
    @Override
    public void paintComponent( Graphics oldG ) {
        Graphics2D g = (Graphics2D)oldG;
        Point2D startPoint = new Point2D.Float( 0, 0 );
        Point2D endPoint = new Point2D.Float( getWidth(), 0 );
        Color back = Color.white;
        if( isDark ) {
            back = Color.black;
        }
        Paint gradientPaint = new GradientPaint( startPoint, back,
                                                 endPoint, getBackground(),
                                                 false );
        g.setPaint( gradientPaint );
        g.fill( new Rectangle2D.Double( 0, 0, getWidth(), getHeight() ) );
    }
 
    /*public static void main( String[] arg ) {
        JFrame f = new JFrame( "GradientPanel Test" );
        f.setDefaultCloseOperation( f.EXIT_ON_CLOSE );
        GradientPanel p = new GradientPanel();
        p.setBackground( Color.green );
        p.add( new JButton( "Button" ) );
        p.add( new JTextField( 12 ) );
        p.setBorder( BorderFactory.createTitledBorder( "Gradient Panel" ) );
        f.getContentPane().add( p );
        f.setSize( 300, 300 );
        f.setVisible( true );
    }*/
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
